<?php
namespace app\common\service;

use app\admin\model\Cate;
use think\facade\Request;
use app\admin\traits\Result;
use think\Db;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/7/13
 * Time: 17:29
 */
class CateService
{

    public static function cate_tree_three($is_nav=''){
        $map['status'] = 1;
        if($is_nav == 1){ //导航栏
            $map['is_nav'] = 1;
        }
        $cate_list = Cate::where('parent_id',0)->field('id,title,type,is_nav')->where($map)->order('sort asc,id asc')->select();
        // ->withAttr('typetitle', function($value, $data) {
        //     return ($data['type']==1)?'单页':(($data['type']==2)?'多文章':'多图');
        // })
        foreach ($cate_list as $key => $val) {
            $cate_list[$key]['typetitle'] = $val['typetitle'];
            $cate_list[$key]['sub'] = Cate::field('id,title,type,is_nav,desc,img')->where('parent_id',$val['id'])->where($map)->order('sort asc,id asc')->select();
            foreach ($cate_list[$key]['sub'] as $key1 => $val1) {
                //顶级分类展示最后一个
                if($key1 == 0){
                    $cate_list[$key]['sub_id'] = $val1['id'];
                }
                //$cate_list[$key]['sub'][$key1]['typetitle'] = $val1['typetitle'];
                $cate_list[$key]['sub'][$key1]['sub'] = Cate::field('id,title,type,is_nav')->where('parent_id',$val1['id'])->where($map)->order('sort asc,id asc')->select();
                foreach ($cate_list[$key]['sub'][$key1]['sub'] as $key2 => $val2) {
                    $cate_list[$key]['sub'][$key1]['sub'][$key2]['typetitle'] = $val2['typetitle'];
                }

            }
        }
        //dump($cate_list);
        return $cate_list;

    }

    public static function cate_tree_two(){
        $cate_list = Cate::field('id,title')->where('parent_id',0)->order('sort asc,id asc')->select();
        foreach ($cate_list as $key => $val) {
			$arr = Cate::field('id,title')->where('parent_id',$val['id'])->order('sort asc,id asc')->select(); 
			$cate_list[$key]['sub'] = $arr??[];
        }

        return $cate_list;

    }

    public static function cate_tree($info,$cid){
        $list = cate_tree_by_cid($info,$cid);
        return $list;
    }

}